<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;
use Admin\Model\LandModel;

class NoticeController extends AuthController {

    //公告列表
    public function notice_list() {
        
        $count = M('notice')->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 5); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $notice = M('notice')->order('sort desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('news', $notice);
        $this->assign('page', $show);
        $this->display();
    }
    

    //添加公告 发送到模板
    public function notice_listadd() {      
        $this->display();
    }
    //修改公告
    public function notice_listedit() {
        $not=M('notice');
        $id=I('id');
        $data=$not->where(array('id' => $id))->find();
        //var_dump($data);
        $this->assign('nlist', $data);
        $this->display();
    }
    //公告修改入库
    public function runnotice_edit() {
        if (!IS_AJAX) {
            $this->error('提交方式不正确', U('Notice/notice_listedit'), 0);
        } else {
            $data=array(
                'id'=>I('id'),
                'notice_title' => I('notice_title'),
                'display' => I('notice_display'),
                'notice_content' => I('kind_text'),
                'addtime' =>date('Y-m-d H:i:s'),
                'sort'=>I('sort')
            );
            M('notice')->save($data);
            $this->success('公告修改成功', U('Notice/notice_list'), 1);
        }
    }
    //删除公告
    public function notice_del(){    
            $id=I('id');
            $nlist=M('notice');
            $nlist->delete($id);
            $this->success('删除成功', U('Notice/notice_list'), 1);
    }
    //排序
    public function noticeorder(){
        if (I('left_id')) {
            $notice = M('notice');
            foreach (I('left_id') as $key => $val) {
                $result = $notice->where('id=' . $val)->setField('sort', I($val));
            }
        }
        //var_dump(U('News/news_column'));exit;
        $this->success('排序成功', U('Notice/notice_list'), 1);
    }
    
    //公告增加入库
    public function runnotice_add() {
        if (!IS_AJAX) {
            $this->error('提交方式不正确', U('Notice/notice_listadd'), 0);
        } else {
            $data = array(
                'id'=>null,
                'notice_title' => I('notice_title'),
                'display' => I('notice_display'),
                'notice_content' => I('kind_text'),
                'addtime' =>date('Y-m-d H:i:s'),
                'sort'=>I('sort')
            );
            M('notice')->add($data);
            $this->success('文章保存成功', U('Notice/notice_list'), 1);
        }
    }

    

    

    

}
